package cn.stylefeng.guns.czlm.controller;

import cn.stylefeng.guns.czlm.entity.TbAttachment;
import cn.stylefeng.guns.czlm.service.TbAttachmentService;
import cn.stylefeng.roses.core.base.controller.BaseController;
import cn.stylefeng.roses.core.reqres.response.ResponseData;
import com.alibaba.fastjson.JSON;
import org.springframework.stereotype.Controller;
import org.springframework.stereotype.Service;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.multipart.MultipartFile;

import javax.annotation.Resource;
import javax.annotation.Resources;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;
import java.io.File;
import java.io.IOException;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.UUID;


@Controller
@RequestMapping("/material")
public class AbAttachmentController extends BaseController {
    private static String PREFIX = "/modular/system/product/";

    @Resource(name="TbAttachmentService")
    private TbAttachmentService tbAttachmentService;


    @RequestMapping("/SelectImg")//查询 Attachment表的图片url
    @ResponseBody
    public String selectImg(HttpSession session) {
        Map<Integer,String> map = new HashMap<>();
       List<TbAttachment> tbAttachmentList= tbAttachmentService.SelectAllImg();
       session.setAttribute("tbAttachmentList",tbAttachmentList);
        return JSON.toJSONString(tbAttachmentList);
    }
    @RequestMapping("/SelectImgTow") //
    @ResponseBody
    public String selectImgTow(HttpSession session,Integer id) {//更换图片拿到id来拿 url
        if(id==null){
            return "false";
        }
        List<TbAttachment> tbAttachmentList=( List<TbAttachment> )  session.getAttribute("tbAttachmentList");
        if(tbAttachmentList==null){
           tbAttachmentList= tbAttachmentService.SelectAllImg();
        }else{
            for (TbAttachment i:tbAttachmentList) {
                if(i.getId().equals(id)){
                    return i.getUrl();
                }
            }
        }
        return "false";
    }

    Long s ;
    @RequestMapping("/saveImg")
    public String saveImg(@RequestParam MultipartFile[] idJustFile,HttpSession session,HttpServletRequest request) throws IOException {
        for (int i = 0; i < idJustFile.length; i++) {
            if(!idJustFile[i].isEmpty()){
                String path = "D:/upload/";
                String fileName = idJustFile[i].getOriginalFilename();
                if(fileName.endsWith(".jpg")||fileName.endsWith(".png")||fileName.endsWith(".bmp")){
                    File file = new File(path, fileName);
                    if (!file.getParentFile().exists()){

                        System.err.println(s);
                        file.getParentFile().mkdir();
                    }
                    s=  file.length();
                    //获取原文件名
                    String oldName=idJustFile[i].getOriginalFilename();
                    //产生一个随机值
                    String name= UUID.randomUUID().toString();
                    //获取图片的后缀
                    String suffix=oldName.substring(oldName.lastIndexOf("."));
                    //组装成新的文件名
                    String newName=name+suffix;
                    //完成文件上传
                    idJustFile[i].transferTo(new File(path+File.separator+newName));
                    System.err.println(path+File.separator+newName);
                    TbAttachment tbAttachment= new TbAttachment();
                    tbAttachment.setSuffix(suffix);
                    tbAttachment.setName(oldName);
                    if(null==s){
                        s=10086L;
                    }
                    tbAttachment.setSize(s);
                    tbAttachment.setId(156);
                    tbAttachment.setUrl(newName);
                    tbAttachment.setMimeType("image/jpeg");
                   session.getAttribute("user");
                    tbAttachmentService.saveImg(tbAttachment);
                }else{
                    //失败返回
                    return PREFIX+"img.html";
                }
            }
        }
        //成功返回
        return PREFIX+"img.html";
    }

    @RequestMapping("/delImg")
    @ResponseBody
    public ResponseData delImg(Integer id) {
        tbAttachmentService.delImg(id);
        return SUCCESS_TIP;
    }


}